package com.miracle.mango.component.security;

import com.alibaba.fastjson.JSON;
import com.miracle.mango.util.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description: 无权访问,无权访问返回的 JSON 格式数据给前端（否则为 403 html 页面）
 * @author: wangxf
 * @date: Created in 2020/8/23 15:38
 */
@Slf4j
@Component
public class AjaxAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        ResultBody responseBody = new ResultBody();
        responseBody.setCode("300");
        responseBody.setMessage("Need Authorities!");
        response.getWriter().write(JSON.toJSONString(responseBody));
    }

}
